package com.jiepos.mpos.pay.quickpay.service.huiepay;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import com.hy.pmp.client.DefaultQuickPayClient;
import com.hy.utils.JsonUtil;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.core.util.GlobalUtils;
import com.jiepos.mpos.core.util.MathUtils;

/**
* 标题:
* 模块: <模块>
* 描 述: <描述>
* 版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
* @author 杨剑武
* @version 1.0
* @since 2018年3月9日
* @history（历次修订内容、修订人、修订时间等）
*/

public class AssembleBusiness {
	public static String assemble1211MsgBody(DefaultQuickPayClient client, Map<String, String> dataMap)
			throws UnsupportedEncodingException {
		String pri_acct_no = dataMap.get("act_out_no"); // 帐号
		String encryAcctNo = client.encrypt(pri_acct_no, SystemConstant.UTF_ENCODING); // 账户信息需要用RSA加密后进行Base64编码
		Map<String, Object> kv = new HashMap<String, Object>();
		String tran_amt = String
				.valueOf(MathUtils.mul(Double.parseDouble(dataMap.get("trans_at")), Double.parseDouble("100")));
		kv.put("tran_cd", "1211"); // 交易类型
		kv.put("prod_cd", GlobalUtils.getPayConfProperty("huiepay.pay.prod_cd")); // 产品编码T1:1140
																					// T0:1171
		kv.put("biz_cd", GlobalUtils.getPayConfProperty("huiepay.pay.biz_cd")); // 0000026业务编码
		kv.put("chn_ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.chn_ins_id_cd")); // 密钥机构号
		kv.put("ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.ins_id_cd")); // 机构代码
		kv.put("mcht_cd", GlobalUtils.getPayConfProperty("huiepay.pay.mcht_cd")); // 商户号
		// todo..................
		kv.put("tran_dt_tm", dataMap.get("loc_dt") + dataMap.get("loc_ts")); // 订单发送时间
		kv.put("order_id", dataMap.get("loc_trace_no")); // 商户订单号
		kv.put("user_id", dataMap.get("trf_mchnt_cd")); // 会员号
		kv.put("req_reserved", ""); // 请求方保留域
		kv.put("pri_acct_no", encryAcctNo);// 帐号
		kv.put("curr_cd", "156"); // 交易币种
		kv.put("tran_amt", tran_amt.substring(0, tran_amt.indexOf("."))); // 交易金额单位为分
		kv.put("order_timeout", GlobalUtils.getPayConfProperty("huiepay.pay.order_timeout")); // 订单支付超时时间
		kv.put("order_desc", "快捷支付"); // 订单信息
		kv.put("noti_url", dataMap.get("payUrl") + GlobalUtils.getPayConfProperty("huiepay.pay.notifyUrl")); // 异步回调地址
		return JsonUtil.toJsonByObj(kv);
	}

	public static String assemble1221MsgBody(DefaultQuickPayClient client, Map<String, String> dataMap)
			throws UnsupportedEncodingException {
		Map<String, Object> kv = new HashMap<String, Object>();
		kv.put("tran_cd", "1221"); // 交易类型
		kv.put("prod_cd", GlobalUtils.getPayConfProperty("huiepay.pay.prod_cd")); // 产品编码T1:1140
																					// T0:1171
		kv.put("biz_cd", GlobalUtils.getPayConfProperty("huiepay.pay.biz_cd")); // 0000026业务编码
		kv.put("chn_ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.chn_ins_id_cd")); // 密钥机构号
		kv.put("ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.ins_id_cd")); // 机构代码
		kv.put("mcht_cd", GlobalUtils.getPayConfProperty("huiepay.pay.mcht_cd")); // 商户号
		kv.put("tran_dt_tm", dataMap.get("loc_dt") + dataMap.get("loc_ts")); // 订单发送时间
		kv.put("order_id", dataMap.get("loc_trace_no")); // 商户订单号
		kv.put("req_reserved", ""); // 请求方保留域
		kv.put("user_id", dataMap.get("trf_mchnt_cd")); // 会员号
		kv.put("curr_cd", "156"); // 交易币种
		String tran_amt = String
				.valueOf(MathUtils.mul(Double.parseDouble(dataMap.get("trans_at")), Double.parseDouble("100")));
		kv.put("tran_amt", tran_amt.substring(0, tran_amt.indexOf("."))); // 交易金额
		kv.put("order_timeout", GlobalUtils.getPayConfProperty("huiepay.pay.order_timeout")); // 订单支付超时时间
		kv.put("order_desc", "快捷签约+支付"); // 订单信息

		String pri_acct_no = dataMap.get("act_out_no"); // 帐号
		String encryAcctNo = client.encrypt(pri_acct_no, SystemConstant.UTF_ENCODING); // 账户信息需要用RSA加密后进行Base64编码
		// 加密银行账号
		kv.put("pri_acct_no", encryAcctNo);

		// 用户身份敏感信息加密
		Map<String, Object> customer_kv = new HashMap<String, Object>();
		customer_kv.put("cert_id", dataMap.get("certificate_no")); // 证件号码
		customer_kv.put("customer_nm", dataMap.get("real_name")); // 姓名
		customer_kv.put("phone_no", dataMap.get("phone")); // 手机号
		customer_kv.put("sms_code", ""); // 短信验证码
		customer_kv.put("cvn2", dataMap.get("cvv2")); // CVN2
		String month = dataMap.get("exptm").substring(0, 2);
		String year = dataMap.get("exptm").substring(2, dataMap.get("exptm").length());
		customer_kv.put("expired", year + month); // 有效期
		customer_kv.put("pin", ""); // 持卡人密码
		String customer_json = JsonUtil.toJsonByObj(customer_kv);
		String customer_encrypt = client.encrypt(customer_json, SystemConstant.UTF_ENCODING); // 对银行卡验证信息及身份信息加密
		kv.put("customer_info", customer_encrypt);
		kv.put("noti_url", dataMap.get("payUrl") + GlobalUtils.getPayConfProperty("huiepay.pay.notifyUrl")); // 异步回调地址
		return JsonUtil.toJsonByObj(kv);
	}

	public static String assemble1231MsgBody(Map<String, String> dataMap, String user_agree_id, String pay_agree_id)
			throws UnsupportedEncodingException {
		Map<String, Object> kv = new HashMap<String, Object>();
		kv.put("tran_cd", "1231");
		kv.put("prod_cd", GlobalUtils.getPayConfProperty("huiepay.pay.prod_cd")); // 产品编码T1:1140
																					// T0:1171
		kv.put("biz_cd", GlobalUtils.getPayConfProperty("huiepay.pay.biz_cd")); // 0000026业务编码
		kv.put("chn_ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.chn_ins_id_cd")); // 密钥机构号
		kv.put("ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.ins_id_cd")); // 机构代码
		kv.put("mcht_cd", GlobalUtils.getPayConfProperty("huiepay.pay.mcht_cd")); // 商户号
		kv.put("tran_dt_tm", dataMap.get("loc_dt") + dataMap.get("loc_ts")); // 订单发送时间
		kv.put("order_id", dataMap.get("loc_trace_no")); // 商户订单号
		kv.put("req_reserved", "");
		kv.put("curr_cd", "156");
		String tran_amt = String
				.valueOf(MathUtils.mul(Double.parseDouble(dataMap.get("trans_at")), Double.parseDouble("100")));
		kv.put("tran_amt", tran_amt.substring(0, tran_amt.indexOf("."))); // 交易金额
		kv.put("user_id", dataMap.get("trf_mchnt_cd")); // 会员号
		kv.put("user_agree_id", user_agree_id);
		kv.put("pay_agree_id", pay_agree_id);
		kv.put("order_timeout", GlobalUtils.getPayConfProperty("huiepay.pay.order_timeout")); // 订单支付超时时间
		kv.put("order_desc", "快捷协议号支付");
		kv.put("noti_url", dataMap.get("payUrl") + GlobalUtils.getPayConfProperty("huiepay.pay.notifyUrl")); // 异步回调地址
		return JsonUtil.toJsonByObj(kv);
	}

	public static String assemble1241MsgBody(DefaultQuickPayClient client, Map<String, String> dataMap)
			throws Exception {
		Map<String, String> kv = new HashMap<String, String>();

		kv.put("tran_cd", "1241"); // 交易类型
		kv.put("prod_cd", GlobalUtils.getPayConfProperty("huiepay.pay.prod_cd")); // 产品编码T1:1140
																					// T0:1171
		kv.put("biz_cd", GlobalUtils.getPayConfProperty("huiepay.pay.biz_cd")); // 0000026业务编码
		kv.put("chn_ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.chn_ins_id_cd")); // 密钥机构号
		kv.put("ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.ins_id_cd")); // 机构代码
		kv.put("mcht_cd", GlobalUtils.getPayConfProperty("huiepay.pay.mcht_cd")); // 商户号
		// todo..................
		kv.put("tran_dt_tm", dataMap.get("loc_dt") + dataMap.get("loc_ts"));
		kv.put("order_id", dataMap.get("loc_trace_no")); // 20170717000006,20170717000015
		kv.put("user_id", dataMap.get("trf_mchnt_cd")); // 会员号
		kv.put("order_desc", "");
		kv.put("req_reserved", "");
		kv.put("check_flg", "1111000000");
		kv.put("risk_inf", "");

		Map<String, Object> customer_kv = new HashMap<String, Object>();
		customer_kv.put("cert_tp", "01"); // 证件类型
		customer_kv.put("cert_id", dataMap.get("certificate_no")); // 证件号码
		customer_kv.put("customer_nm", dataMap.get("real_name")); // 姓名
		customer_kv.put("phone_no", dataMap.get("phone")); // 手机号
		customer_kv.put("sms_code", ""); // 短信验证码
		customer_kv.put("cvn2", dataMap.get("cvv2")); // CVN2
		String month = dataMap.get("exptm").substring(0, 2);
		String year = dataMap.get("exptm").substring(2, dataMap.get("exptm").length());
		customer_kv.put("expired", year + month); // 有效期
		String customer_encrypt = client.encrypt(JsonUtil.toJsonByObj(customer_kv), SystemConstant.UTF_ENCODING); // 对银行卡验证信息及身份信息加密
		kv.put("customer_info", customer_encrypt);
		return JsonUtil.toJsonByObj(kv);
	}

	public static String assemble1251MsgBody(DefaultQuickPayClient client, Map<String, String> dataMap)
			throws UnsupportedEncodingException {
		Map<String, String> kv = new HashMap<String, String>();
		kv.put("tran_cd", "1251"); // 交易类型
		kv.put("prod_cd", GlobalUtils.getPayConfProperty("huiepay.pay.prod_cd")); // 产品编码T1:1140
																					// T0:1171
		kv.put("biz_cd", GlobalUtils.getPayConfProperty("huiepay.pay.biz_cd")); // 0000026业务编码
		kv.put("chn_ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.chn_ins_id_cd")); // 密钥机构号
		kv.put("ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.ins_id_cd")); // 机构代码
		kv.put("mcht_cd", GlobalUtils.getPayConfProperty("huiepay.pay.mcht_cd")); // 商户号
		kv.put("tran_dt_tm", dataMap.get("loc_dt") + dataMap.get("loc_ts"));
		kv.put("order_id", dataMap.get("loc_trace_no")); // 20170717000006,20170717000015
		kv.put("user_id", dataMap.get("trf_mchnt_cd")); // 会员号
		kv.put("order_desc", "");
		kv.put("req_reserved", "");
		kv.put("check_flg", "1111000000");
		kv.put("risk_inf", "");

		Map<String, Object> customer_kv = new HashMap<String, Object>();
		customer_kv.put("cert_tp", "01"); // 证件类型
		customer_kv.put("cert_id", dataMap.get("certificate_no")); // 证件号码
		customer_kv.put("customer_nm", dataMap.get("real_name")); // 姓名
		customer_kv.put("phone_no", dataMap.get("phone")); // 手机号
		customer_kv.put("sms_code", dataMap.get("verify_code")); // 短信验证码
		customer_kv.put("cvn2", dataMap.get("cvv2")); // CVN2
		String month = dataMap.get("exptm").substring(0, 2);
		String year = dataMap.get("exptm").substring(2, dataMap.get("exptm").length());
		customer_kv.put("expired", year + month); // 有效期
		customer_kv.put("pin", ""); // 持卡人密码

		String customer_json = JsonUtil.toJsonByObj(customer_kv);
		String customer_encrypt = client.encrypt(customer_json, SystemConstant.UTF_ENCODING); // 对银行卡验证信息及身份信息加密

		kv.put("customer_info", customer_encrypt);

		return JsonUtil.toJsonByObj(kv);
	}

	public static String assemble5091MsgBody(Map<String, String> dataMap) {
		Map<String, String> kv = new HashMap<String, String>();
		kv.put("tran_cd", "5091"); // 交易类型
		kv.put("prod_cd", GlobalUtils.getPayConfProperty("huiepay.pay.prod_cd")); // 产品编码T1:1140
																					// T0:1171
		kv.put("biz_cd", GlobalUtils.getPayConfProperty("huiepay.pay.biz_cd")); // 0000026业务编码
		kv.put("chn_ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.chn_ins_id_cd")); // 密钥机构号
		kv.put("ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.ins_id_cd")); // 机构代码
		kv.put("mcht_cd", GlobalUtils.getPayConfProperty("huiepay.pay.mcht_cd")); // 商户号
		kv.put("tran_dt_tm", dataMap.get("loc_dt") + dataMap.get("loc_ts"));
		kv.put("order_id", dataMap.get("loc_trace_no")); // 20170717000006,20170717000015
		// kv.put("orig_sys_order_id", "201707170304046");
		kv.put("req_reserved", "");

		String result = JsonUtil.toJsonByObj(kv);
		return result;
	}
}
